package com.zhiche.lisa.bms.service.capital.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.capital.CapitalArrDtlMapper;
import com.zhiche.lisa.bms.dao.model.capital.CapitalArrDtl;
import com.zhiche.lisa.bms.pojo.dto.util.FcPayApplyReturnDTO;
import com.zhiche.lisa.bms.service.capital.CapitalArrDtlService;
import com.zhiche.lisa.bms.service.utils.PayApiUtil;
import com.zhiche.lisa.core.supports.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 排款计划明细表新 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-12-17
 */
@Service
@Slf4j
public class CapitalArrDtlServiceImpl extends ServiceImpl<CapitalArrDtlMapper, CapitalArrDtl> implements CapitalArrDtlService {
    @Autowired
    private PayApiUtil payApiUtil;

    /**
     * 根据条件查询排款计划明细
     *
     * @param condition
     * @return
     */
    @Override
    public List<CapitalArrDtl> selectCapitalArrDtlList(Map<String, Object> condition) {
        return this.selectList(new EntityWrapper<>());
    }

    /**
     * 批量新增
     *
     * @param capitalArrDtls
     */
    @Override
    public void insertCapitalArrDtls(List<CapitalArrDtl> capitalArrDtls) {
        this.insertBatch(capitalArrDtls);
    }

    /**
     * 根据headId获取对应条件的排款计划明细
     *
     * @param headId
     * @return
     */
    @Override
    public List<CapitalArrDtl> selectCapitalArrDtlListByHeadId(Long headId) {
        EntityWrapper<CapitalArrDtl> ew = new EntityWrapper<>();
        ew.eq("head_id", headId);
        return this.selectList(ew);
    }

    /**
     * 根据油卡支付的考核状态对应修改排款详明状态
     */
    @Override
    public void updateStateByFcPay(List<Long> headId) {
        try {
            EntityWrapper<CapitalArrDtl> capitalArrDtlEntityWrapper = new EntityWrapper<>();
            capitalArrDtlEntityWrapper.ne("sync_status", 10);
            capitalArrDtlEntityWrapper.isNotNull("cost_id");
            if (!CollectionUtils.isEmpty(headId)) {
                capitalArrDtlEntityWrapper.in("head_id", headId);
            }
            List<CapitalArrDtl> capitalArrDtls = this.selectList(capitalArrDtlEntityWrapper);
            if (!CollectionUtils.isEmpty(capitalArrDtls)) {
                capitalArrDtls.stream()
                        .filter(capitalArrDtl -> !StringUtils.isEmpty(capitalArrDtl.getCostId()))
                        .forEach(capitalArrDtl -> {
                            FcPayApplyReturnDTO fcPayApplyReturnDTO = payApiUtil.getPayApplyAudit(capitalArrDtl.getCostId()).getData();
                            capitalArrDtl.setSyncStatus(fcPayApplyReturnDTO.getAuditStatusCode());
                        });
                this.updateBatchById(capitalArrDtls);
            }
        } catch (BaseException e) {
            log.error("CapitalArrDtlServiceImpl.updateStateByFcPay error: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("CapitalArrDtlServiceImpl.updateStateByFcPay error: {}", e.getMessage());
            throw new BaseException("更新状态异常");
        }
    }
}
